using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Dashboard
{
	public class TcpConnection : Connection
	{
		private const int port = 5369;
		
		private TcpClient client;
		private NetworkStream stream;
		
		public TcpConnection ()
		{
			client = new TcpClient ();
			client.Connect (new IPEndPoint (IPAddress.Loopback, port));

			stream = client.GetStream ();
		}
		
		public override void Send (Message message)
		{
			Message.Serializer.Serialize (stream, message);

			// 0xff indicates end-of-message
			stream.WriteByte (0xff);
		}

		public override void Subscribe (string pattern, MessageHandler handler)
		{
			throw new NotImplementedException ();
		}

		public override void Dispose ()
		{
			stream.Close ();

			client.Close ();
			client = null;
		}

		private class TcpConnectionHandler : XmlConnectionHandler
		{
			private TcpConnectionHandler (TcpClient client)
			{

			}

			protected override Stream Stream
			{
				get { return null; }
			}

			protected override bool Closed
			{
				get { return false; }
			}

			public override void Close ()
			{

			}
		}
	}
}
